import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;


public class ConfigurationManager {
	private static String PROPERTY_FILE_NAME = "Config.ini" ;
	
	private static Properties properties; 
	private Integer portNumber ;
	private static  Logger log = Logger.getLogger();
	private String dbfile;
	private String bindName;
	
	private static ConfigurationManager propertymanager;
	
	public static  Properties getProperties() throws IOException
	{
		if(properties != null)
			return properties;
		
		properties = new Properties();
	    try {	
	        	properties.load(new FileInputStream(PROPERTY_FILE_NAME));
	    	} catch (IOException e) {
	    		log.error("Could not load properties file:"+ e.getMessage());
	    		throw e;
	    		
	    	}
	    
	    return properties;
	}
	
	public static ConfigurationManager getPropertyManager()
	{
		if(propertymanager == null)
		{
			propertymanager = new ConfigurationManager();
		}
		
		return propertymanager;
	}
	
	public  Integer getPortNumber() throws IOException
	{
		if (portNumber == null)
			{
				try{
					portNumber = Integer.parseInt(getProperties().getProperty("PortNumber"));
					}	
				catch(NumberFormatException e)
					{
						log.error("Preffered Neighbours Must be an Integer");
					}
			}
		
		return portNumber; // returns null if error
	}
	
	public String getDBFile() throws IOException
	{
		if(dbfile == null)
		{
			dbfile = getProperties().getProperty("DBFile");
		}
		
		return dbfile;
	}
	
	public String getBindName() throws IOException
	{
		if(bindName == null)
		{
			bindName = getProperties().getProperty("BIND-NAME");
		}
		return bindName;
	}
}
